//入口函数
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
 var cartcount = JSON.parse(localStorage.getItem('cartId') || '[]')
var store = new Vuex.Store({
    state:{
        cartcount:cartcount
        // count:0
    },
    mutations:{
        addcart(state,shopinfo){
            var flag = false
            state.cartcount.some(item => {
                if(item.id == shopinfo.id){
                    item.count += parseInt(shopinfo.count)
                    flag = true
                    return true
                }
            })
            if(!flag){ 
                state.cartcount.push(shopinfo)
            }
            localStorage.setItem('cartId',JSON.stringify(state.cartcount))         
        },
        updatecartcount(state,obj){
            state.cartcount.some(item => {
                if(item.id == obj.id){
                    item.count = parseInt(obj.count)
                    return true
                }
            })
            localStorage.setItem('cartId',JSON.stringify(state.cartcount)) 
        },
        removeshop(state,id){
            state.cartcount.some((item,i) => {
                if(item.id == id){
                    state.cartcount.splice(i,1)
                }
            })
            localStorage.setItem('cartId',JSON.stringify(state.cartcount))
        },
        updatecartselected(state,info){
            state.cartcount.some(item => {
                if(item.id == info.id){
                    item.selected = info.selected
                    return true
                }
            })
            localStorage.setItem('cartId',JSON.stringify(state.cartcount))
        }
    },
    getters:{
        getAllCount(state){
            var sumcount = 0
            state.cartcount.forEach(element => {
                sumcount += parseInt(element.count)
                // return sumcount
            });
            return sumcount
        },
        getcartcount(state){
            var o = {}
            state.cartcount.forEach(item => {
                o[item.id] = item.count
            })
            return o
        },
        getshopselected(state){
            var o = {}
            state.cartcount.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getshopcountAndamount(state){
            var o = {
                count:0,
                amount:0
            }
            state.cartcount.forEach(item => {
                if(item.selected){
                    o.count += item.count
                    o.amount += item.count*item.price
                }
            })
            return o
        }
    }
})
//定义全局过滤器
Vue.filter('dataFormat',function(dataStr){
        var date = new Date(dataStr)
        var YYYY = date.getFullYear()
        var MM = date.getMonth() + 1
        var DD = date.getDate()
        var hh = date.getHours()
        var mm = date.getMinutes()
        var ss = date.getSeconds()
        return `${YYYY}-${MM}-${DD} ${hh}:${mm}:${ss}`
})
//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
import './mui/css/mui.min.css'
import './mui/css/icons-extra.css'
//按需导入mint-ui中的组件
// import { Header,Swipe,SwipeItem,Button,Lazyload } from 'mint-ui'
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name,Swipe)
// Vue.component(SwipeItem.name,SwipeItem)
// Vue.component(Button.name,Button)
// Vue.component(Lazyload.name,Lazyload)

import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'
//引入路由
import router from './router.js'
import app from './app.vue'
var vm = new Vue({
    el:'#app',
    render:c => c(app),
    router,
    store
})